//
//  YZMAnnouncementListModel.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 16/4/28.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMAnnouncementListModel.h"
#import "Mantle+Utils.h"

@implementation YZMAnnouncementListModel

+ (NSDictionary *)JSONKeyPathsByPropertyKey {

    return @{
             @"noticeContent": @"noticeContent",
             @"noticeTitle": @"noticeTitle",
             @"noticeType": @"noticeType",
             @"releaseTime": @"releaseTime"
             };
}

+ (NSValueTransformer *)releaseTimeJSONTransformer {

    return [NSValueTransformer valueTransformerForName: MTLDoubleDateTransformerName];
}

- (void)setNilValueForKey:(NSString *)key{

}

- (NSMutableAttributedString *)attributeWithHTML {
    
    NSString *contentStr = [NSString stringWithFormat:@"<style>a,html{color:#FFF}</style><font size=\"4\">%@</font>", self.noticeContent];
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithData:[contentStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];

    return attrStr;
}

+ (void)createEncoder:(NSArray *)array forKey:(NSString *)keyName {
    
    if (!array || array.count == 0 || !keyName) {
        
        return;
    }
    
    NSMutableArray *archiverArray = [NSMutableArray array];
    
    for (YZMAnnouncementListModel *instance in array) {
        
        NSMutableData *data = [NSMutableData data];
        //创建一个归档类
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
        [archiver encodeObject:instance forKey:keyName];
        [archiver finishEncoding];
        
        [archiverArray addObject:data];
    }
    
    //将数据写入文件里
    [[NSUserDefaults standardUserDefaults] setObject:archiverArray forKey:keyName];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSMutableArray *)createDecoder:(NSString *)keyName {
    
    if (!keyName) {
        
        return nil;
    }
    
    NSMutableArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:keyName];
    
    if (!array ||array.count == 0) {
        
        return nil;
    }
    
    NSMutableArray *tempArray = [NSMutableArray array];
    
    for (NSData *data in array) {
        
        if (data && data.length != 0) {
            
            NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
            
            YZMAnnouncementListModel *instance = [unarchiver decodeObjectForKey:keyName];
            [unarchiver finishDecoding];
            
            [tempArray addObject:instance];
        }
    }
    
    if (tempArray.count == 0) {
        
        return nil;
    }
    
    return tempArray;
}

@end
